package com.whmskj.xjlhsc.schedul;

import cn.hutool.core.collection.CollectionUtil;
import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.whmskj.xjlhbc.db.DataSourceType;
import com.whmskj.xjlhbc.db.DynamicDataSourceContextHolder;
import com.whmskj.xjlhsc.jitapi.entity.Device;
import com.whmskj.xjlhsc.jitapi.mapper.DeviceMapper;
import com.whmskj.xjlhsc.schedul.async.po.YiqianjiReport;
import com.whmskj.xjlhsc.schedul.async.service.YiqianjiReportService;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.LocalTime;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * @BelongsProject: XJLHSC
 * @BelongsPackage: com.whmskj.xjlhsc.schedul
 * @Author: lk
 * @CreateTime: 2025-07-14
 * @Description: 异迁机日报
 * @Version: 1.0
 */
@Component
@Slf4j
@RequiredArgsConstructor
public class YiQianJiReportTask implements Task {

    private final static String DEVICE_NAME = "异纤机";
    private final static String TB_PREFIX = "production_statistic_table%s";

    private final DeviceMapper deviceMapper;
    private final YiqianjiReportService yiqianjiReportService;

    @Scheduled(cron = "0 59 23 * * ?")
    @Override
    public void taskGenerate() {
        LocalDate today = LocalDate.now();
//        LocalDate today = LocalDate.of(2025, 7, 11);
        log.info("生成{}异迁机日报", today);
        List<Device> yqjList = deviceMapper.findByDeviceName(DEVICE_NAME);
        DynamicDataSourceContextHolder.setDataSourceType(DataSourceType.SC.name());

        List<YiqianjiReport> reports = new ArrayList<>();
        for (Device device : yqjList) {
            YiqianjiReport report = yiqianjiReportService.rpQuery(LocalDateTime.of(today, LocalTime.MIN), LocalDateTime.of(today, LocalTime.MAX), String.format(TB_PREFIX, device.getMachineNumber()));
            if (ObjectUtil.isNull(report)) continue;
            report.setDateYmd(today.toString());
            report.setDeviceId(device.getDeviceId());
            report.setDeviceName(device.getDeviceName() + device.getDeviceNo());
            reports.add(report);
        }

        reports.forEach(cur -> cur.setDateYmd(today.toString()));
        DynamicDataSourceContextHolder.setDataSourceType(DataSourceType.MASTER.name());
        LambdaQueryWrapper<YiqianjiReport> qe = new LambdaQueryWrapper<>();
        qe.eq(YiqianjiReport::getDateYmd, today.toString());
        List<YiqianjiReport> list = yiqianjiReportService.list(qe);
        //如果当前的机器数据已经统计过了，直接在原来的数据上修改
        if (CollectionUtil.isNotEmpty(list)) {
            Map<Integer, Integer> collect = list.stream().collect(Collectors.toMap(YiqianjiReport::getDeviceId, YiqianjiReport::getId));
            reports.forEach(cur -> cur.setId(collect.get(cur.getDeviceId())));
        }
        yiqianjiReportService.saveOrUpdateBatch(reports);

    }


}
